<template>
	<view>
		<tn-nav-bar fixed customBack :bottomShadow="false" backgroundColor="#609B22">
		  <view slot="back" class='tn-custom-nav-bar__back'
		    @click="goBack">
		    <text class='icon tn-icon-left-arrow'></text>
		  </view>
		  <view class="tn-flex tn-flex-col-center tn-flex-row-center " style="color: #fff;">
		    <text  class="tn-text-bold tn-text-xl tn-color-black">{{loginIndex==0?'登录':'注册'}}</text>
		  </view>
		</tn-nav-bar>
		<view v-if="loginIndex == 0">
			<view class="container" >
				<view class="container-bg"></view>
				<view class="container-form">
					<view class="container-form-item">
						<icon class="iconfont icon-shouji"></icon>
						<input v-model="ufrom.account" placeholder="请输入账号"/>
					</view>
					<view class="container-form-item">
						<icon class="iconfont icon-mima"></icon>
						<input  v-model="ufrom.pwd" type="password" placeholder="请输入密码"/>
						<icon :class="switchPwd?'iconfont icon-chakan':'iconfont icon-yincang'" @click="handleSwitchPwd"></icon>
					</view>
					<button class="container-form-submit" @click="login">登录</button>
				</view>
				
			</view>
			<view style="position: fixed; bottom: 130px; width: 100%; ">
				<view style="text-align: center;">
					没有账号？<text style="color: #4687ff;" @click="loginIndex = 1">去注册</text>
				</view>
			</view>
		</view>
		<view v-else>
			<view  class="container">
				<view class="container-bg"></view>
				<view class="container-form">
					<view class="container-form-item">
						<icon class="iconfont icon-shouji"></icon>
						<input v-model="ufrom.account" placeholder="请输入账号"/>
					</view>
					<view class="container-form-item">
						<icon class="iconfont icon-mima"></icon>
						<input  v-model="ufrom.pwd" type="password" placeholder="请输入密码"/>
						<icon :class="switchPwd?'iconfont icon-chakan':'iconfont icon-yincang'" @click="handleSwitchPwd"></icon>
					</view>
					<view class="container-form-item">
						<icon class="iconfont icon-shouji"></icon>
						<input v-model="ufrom.code" placeholder="请输入邀请码"/>
					</view>
					<button class="container-form-submit" @click="save">注册</button>
				</view>
			</view>
			<view style="position: fixed; bottom: 90px; width: 100%; ">
				<view style="text-align: center;">
					已有账号？<text style="color: #4687ff;" @click="loginIndex = 0">去登录</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				loginIndex:0,
				switchPwd:false,//查看隐藏密码
				ufrom:{
					account:'',
					pwd:'',
					code:''
				}
			};
		},
		methods:{
			goBack(){
					  uni.navigateBack({delta:1})
			},
			handleSwitchPwd(){
				this.switchPwd=!this.switchPwd;
			},
			save(){
				if(!this.ufrom.account) return uni.showToast({
					icon:"none",title:"请输入账号！"
				})
				if(!this.ufrom.pwd) return uni.showToast({
					icon:"none",title:"请输入密码！"
				})
				if(!this.ufrom.code) return uni.showToast({
					icon:"none",title:"请输入验证码！"
				})
				uni.request({
					url:'http://mao.zhimulp.online/admin/mao/save',
					method:"POST",
					data:this.ufrom,
					success: (res) => {
						console.log('res',res)
						if(res.data.code!=200){
							uni.showToast({
								icon:"error",
								title:res.data.msg
							})
							return;
						}
						this.loginIndex = 0
					
					}
				})
			},
			login(){
				if(!this.ufrom.account) return uni.showToast({
					icon:"none",title:"请输入账号！"
				})
				if(!this.ufrom.pwd) return uni.showToast({
					icon:"none",title:"请输入密码！"
				})
				uni.request({
					url:'http://mao.zhimulp.online/admin/mao/login',
					method:"POST",
					data:this.ufrom,
					success: (res) => {
						console.log('res',res)
						if(res.data.code!=200){
							uni.showToast({
								icon:"error",
								title:res.data.msg
							})
							return;
						}
						uni.reLaunch({
							url:"/pages/index/index"
						})
						uni.setStorageSync('token',res.data.data.token)
						uni.setStorageSync('userInfo',res.data.data.userInfo)
					}
				})
			}
		}
	}
</script>

<style lang="less">
	/* 胶囊*/
	.tn-custom-nav-bar__back {
	  width: 60%;
	  height: 100%;
	  position: relative;
	  display: flex;
	  justify-content: space-evenly;
	  align-items: center;
	  box-sizing: border-box;
	  background-color: rgba(0, 0, 0, 0.15);
	  border-radius: 1000rpx;
	  border: 1rpx solid rgba(255, 255, 255, 0.5);
	  color: #FFFFFF;
	  font-size: 18px;
	  
	  .icon {
	    display: block;
	    flex: 1;
	    margin: auto;
	    text-align: center;
	  }
	  
	}
	
	/* 间隔线 start*/
	.tn-strip-bottom-min {
	  width: 100%;
	  border-bottom: 1rpx solid #F8F9FB;
	}
	
	.tn-strip-bottom {
	 width: 100%;
	 border-bottom: 20rpx solid rgba(241, 241, 241, 0.8);
	}
	 /* 间隔线 end*/
.container{
	background-color: #F6F6F6;
	width: 100%;
	height: 100vh;
	display: flex;
	align-items: center;
	justify-content: center;
	&-bg{
		width:100%;
		height: 50%;
		background-color: #609B22;
		border-bottom-left-radius: 10%;
		border-bottom-right-radius: 10%;
		position: absolute;
		top:0;
		z-index: 1;	
	}
	&-form{
		width: 90%;
		background-color: white;
		box-sizing: border-box;
		padding:80rpx 20rpx;
		border-radius: 5%;
		z-index: 1;	
		&-item{
			display: flex;
			flex-direction: row;
			padding: 60rpx 0;
			border-bottom: 1px solid gainsboro;
			.iconfont{
				font-size: 40rpx;
				margin-right: 10rpx;
				margin-top:5rpx;
			}
			&-input{
				width: 70%;
				margin-right: 10%;
			}
		}
		&-submit{
			margin-top: 100rpx !important;
			color:white;
			background-color: #82BE20;
			border:none;
		}
	}
}

</style>
